package com.xnyzc.lhy.mis.controller.cmsapi.banner;


import com.xnyzc.lhy.common.entity.BannerRv;
import com.xnyzc.lhy.common.exception.BannerException;
import com.xnyzc.lhy.common.penum.banner.BannerErrorCode;
import com.xnyzc.lhy.mis.entity.param.banner.BannerChangeStatusParam;
import com.xnyzc.lhy.mis.entity.param.banner.BannerSaveParam;
import com.xnyzc.lhy.mis.entity.param.banner.BannerSearchParam;
import com.xnyzc.lhy.mis.entity.result.banner.BannerSearchResult;
import com.xnyzc.lhy.mis.service.banner.IHomePageBannerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 首页轮播图 前端控制器
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-28
 */
@RestController
@RequestMapping("/banner/homePageBanner")
@Slf4j
public class HomePageBannerController {
    @Autowired
    IHomePageBannerService iHomePageBannerService;

    /**
     * banner列表查询
     *
     * @return
     */
    @ApiOperation(value = "banner列表查询", notes = "banner列表查询")
    @PostMapping(value = "/queryBannerList")
    public BannerRv queryBannerList(@RequestBody BannerSearchParam bannerSearchParam) {
        try {
            List<BannerSearchResult> bannerSearchResultList = iHomePageBannerService.queryBannerList(bannerSearchParam);
            return BannerRv.wrapSuccess(bannerSearchResultList);
        }
        catch (BannerException ae){
            log.error("banner列表查询 失败：" + ae.getErrno().getDesc());
            return BannerRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("banner列表查询 失败：" + BannerErrorCode.unKnow.getDesc());
            return BannerRv.wrapErrorCode(BannerErrorCode.unKnow);
        }
    }

    /**
     * banner启动停用
     *
     * @return
     */
    @ApiOperation(value = "banner启动停用", notes = "banner启动停用")
    @PostMapping(value = "/changeBannerStatus")
    public BannerRv changeBannerStatus(@RequestBody BannerChangeStatusParam bannerChangeStatusParam) {
        try {
            iHomePageBannerService.changeBannerStatus(bannerChangeStatusParam);
            return BannerRv.wrapSuccess();
        }
        catch (BannerException ae){
            log.error("banner启动停用 失败：" + ae.getErrno().getDesc());
            return BannerRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("banner启动停用 失败：" + BannerErrorCode.unKnow.getDesc());
            return BannerRv.wrapErrorCode(BannerErrorCode.unKnow);
        }
    }

    /**
     * banner新增编辑
     *
     * @return
     */
    @ApiOperation(value = "banner新增编辑", notes = "banner新增编辑")
    @PostMapping(value = "/saveBanner")
    public BannerRv saveBanner(@RequestBody BannerSaveParam bannerSaveParam) {
        try {
            iHomePageBannerService.saveBanner(bannerSaveParam);
            return BannerRv.wrapSuccess();
        }
        catch (BannerException ae){
            log.error("banner新增编辑 失败：" + ae.getErrno().getDesc());
            return BannerRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("banner新增编辑 失败：" + BannerErrorCode.unKnow.getDesc());
            return BannerRv.wrapErrorCode(BannerErrorCode.unKnow);
        }
    }
}
